//
//  AliyunVODSDKHelper.m
//  Unity-iPhone
//
//  Created by tackor on 2019/6/20.
//

#import "AliyunVODSDKHelper.h"
#import <VODUpload/VODUploadSVideoClient.h>

@interface AliyunVODSDKHelper() <VODUploadSVideoClientDelegate>

@property (nonatomic, strong) VODUploadSVideoClient *uploader;


@property (nonatomic, strong) NSString *curAccessKeyId;
@property (nonatomic, strong) NSString *curAccessKeySecret;
@property (nonatomic, strong) NSString *curAccessToken;

@end

@implementation AliyunVODSDKHelper


-(void)PushVideoSTSWithAccessKeyId:(NSString *)accessKeyId AccessKeySecret:(NSString *)accessKeySecret AccessToken:(NSString *)accessToken
                             Title:(NSString *)title Desc:(NSString *)desc CateId:(NSString *)cateId Tags:(NSString *)tags
                         VideoPath:(NSString *)videoPath ImagePath:(NSString *)imagePath {
    
    _curAccessKeyId = accessKeyId;
    _curAccessKeySecret = accessKeySecret;
    _curAccessToken = accessToken;
    
    self.uploader = [VODUploadSVideoClient new];
    self.uploader.delegate = self;
    
    // weakself
    __weak typeof(self) weakSelf = self;
    
    VodSVideoInfo *info = [VodSVideoInfo new];
    info.title = title;
    info.desc = desc;
    NSInteger num = [cateId integerValue];
    info.cateId = @(num);
    info.tags = tags;
    
    [self.uploader uploadWithVideoPath:videoPath imagePath:imagePath svideoInfo:info accessKeyId:accessKeyId accessKeySecret:accessKeySecret accessToken:accessToken];
    
}


- (void)uploadFailedWithCode:(NSString *)code message:(NSString *)message {
    NSLog(@"failed -------------->>> ");
}

- (void)uploadProgressWithUploadedSize:(long long)uploadedSize totalSize:(long long)totalSize {
    NSLog(@"progress -------------->>> %i, %i", uploadedSize, totalSize);
}

- (void)uploadRetry {
    NSLog(@"retry -------------->>> ");
}

- (void)uploadRetryResume {
    NSLog(@"retry resume -------------->>> ");
}

- (void)uploadSuccessWithResult:(VodSVideoUploadResult *)result {
    
    NSLog(@"result.videoId= %@, result.imageUrl= %@", result.videoId, result.imageUrl);
    
    UnitySendMessage("AliyunVodHelper", "PushSucessCallBack", [[result.videoId stringByAppendingFormat:@"_%@", result.imageUrl] UTF8String]);
}

//凭证过期
- (void)uploadTokenExpired {
    NSLog(@"token Expired -------------->>> ");
    [self.uploader refreshWithAccessKeyId:_curAccessKeyId accessKeySecret:_curAccessKeySecret accessToken:_curAccessToken expireTime:@"1800"];
}


@end
